package com.micro.common.exception;

import com.micro.common.constant.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author pan.shun
 * @version 2.0
 * @since 2021/11/8 22:14
 */
@Slf4j
@SuppressWarnings("unchecked")
@ControllerAdvice
public class MyExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public R exceptionHandler(Exception e) {
        log.error("全局异常信息：{}", e);
        if (e instanceof NotAuthException) {
            return R.forbidden();
        } else {
            return R.error(e.getMessage());
        }
    }
}